﻿using System;
using System.Activities.Tracking;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YasharEl.Infrastructure.Diagnostics.Logging;

namespace YasharEl.Infrastructure.WorkflowModel.Diagnostics
{
    public class LoggingTrackingParticipant : TrackingParticipant
    {
        private readonly ILogger<LoggingTrackingParticipant> logger;

        public LoggingTrackingParticipant(ILogger<LoggingTrackingParticipant> logger)
        {
            Contract.Requires<ArgumentNullException>(logger != null, "logger could not be null.");

            this.logger = logger;
        }

        protected override void Track(TrackingRecord record, TimeSpan timeout)
        {
            if (record != null)
            {
                if (record is WorkflowInstanceRecord)
                {
                    WorkflowInstanceRecord instanceRecord = record as WorkflowInstanceRecord;

                    logger.Debug("Workflow {0} in state {1}, event time : {2}, activity definition {3}",
                        instanceRecord.InstanceId, instanceRecord.State, instanceRecord.EventTime, instanceRecord.ActivityDefinitionId
                    );
                }
                else if (record is ActivityStateRecord)
                {
                    ActivityStateRecord activityRecord = record as ActivityStateRecord;
                    logger.Debug("Activity: {0} ActivityInstanceId: {1} State : {2}", 
                        activityRecord.Activity.Name, activityRecord.InstanceId, activityRecord.State);
                }
            }
        }
    }
}
